package TP05;

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import javax.swing.JComponent;

public class MyThirdWindow extends JFrame {

    JMenuItem btQuitter, btAjouterRectangle, btSupprRectangle;
    JMenu m1;
    JMenuBar mBar;
    JToolBar toolbar;
    boolean dessineRectangle = false;

    public MyThirdWindow(String title) {

        this.setTitle(title);
        this.setSize(500, 400);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setVisible(true);

        ImageIcon icone = new ImageIcon("test.gif");

        JButton myFirstButton = new JButton("OK");
        myFirstButton.setToolTipText("Ceci est une aide ...");

        myFirstButton.setIcon(icone);
        toolbar = new JToolBar("Barre d'outils");
        toolbar.add(myFirstButton);
        toolbar.addSeparator();

        mBar = new JMenuBar();
        m1 = new JMenu("File");
        btQuitter = new JMenuItem("Quitter");
        btAjouterRectangle = new JMenuItem("Ajouter Rectangle");
        btSupprRectangle = new JMenuItem("Supprimer Rectangle");


        btQuitter.addActionListener(actionExit);
        btAjouterRectangle.addActionListener(actionDessiner);
        btSupprRectangle.addActionListener(actionSupprimer);

        //this.getContentPane().add(paintComponent(new Graphics()));


        m1.add(btAjouterRectangle);
        m1.add(btSupprRectangle);
        m1.add(btQuitter);
        mBar.add(m1);  // ajout de menus la barre de menus
        setJMenuBar(mBar);

        this.getContentPane().add(mBar);
        this.getContentPane().setLayout(new FlowLayout(0));
        this.getContentPane().add(toolbar, BorderLayout.NORTH);
    }

    //  utile pour quitter l'application.
    ActionListener actionExit = new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            System.exit(0);
        }
        };
    //  utile pour quitter l'application.
    ActionListener actionDessiner = new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            dessineRectangle = true;
        }
        };
    //  utile pour quitter l'application.
    ActionListener actionSupprimer = new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            dessineRectangle = false;
        }
        };

    public void repaint(Graphics g) {
        paintComponent(g);
    }

    public void update(Graphics g) {
        paintComponent(g);
    }

    public void paintComponent(Graphics g) {
        //Dessiner rectangle
        // if(dessineRectangle)
        //{
        Graphics2D g2 = (Graphics2D) g;
        g2.setColor(Color.BLUE);
        Dimension compDim = getSize();
        Rectangle rectangle = new Rectangle((int) compDim.getWidth() / 4, (int) compDim.getHeight() / 4, (int) compDim.getWidth() / 2, (int) compDim.getHeight() / 2);
        g2.fill(rectangle);
    }

    public static void main(String[] args) {
        MyThirdWindow test = new MyThirdWindow("ihi");
    }
};
